import lombok.extern.log4j.Log4j2;
import mapStruct.dto.RoleDTO;
import mapStruct.dto.UpdateEstimateDTO;
import mapStruct.dto.UserRoleDTO;
import mapStruct.entity.*;
import mapStruct.mapper.EstimateMapper;
import mapStruct.mapper.RoleMapper;
import mapStruct.mapper.UserRoleMapper;
import mapStruct.vo.UserVo;
import mapStruct.vo.UserVo1;
import mapStruct.vo.UserVo2;
import org.junit.Before;
import org.testng.annotations.Test;


import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
public class test {

    String dateString = "20240401";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    LocalDate date = LocalDate.parse(dateString, formatter);
    User user = null;
    @Before
    public void before() {
        Role role = new Role(2L,"admin","超级管理员");
        user = new User(1L,"myName","myPassword","myPhone","myEmail",date,role,BigDecimal.valueOf(0.212));
    }

    /**相同属性名*/
    @Test
    public void test(){
        Role role = new Role(2L,"admin","超级管理员");
        user = new User(1L,"myName","myPassword","myPhone","myEmail",date,role,BigDecimal.valueOf(0.212));

        UserRoleMapper userRoleMapperInstance =  UserRoleMapper.INSTANCE;
        UserRoleDTO userRoleDTO = userRoleMapperInstance.defaultConvert();
        UserRoleDTO userRoleDTO1 = userRoleMapperInstance.toUserRoleDTO(user);
        System.out.println("userRoleDTO============"+userRoleDTO);
        System.out.println("userRoleDTO1=========="+userRoleDTO1);
    }

    /**参数做值*/
    @Test
    public void test1() {
        Role role = new Role(2L,"admin","超级管理员");
        user = new User(1L,"myName","myPassword","myPhone","myEmail",date,role,BigDecimal.valueOf(0.212));

        UserRoleMapper instance = UserRoleMapper.INSTANCE;
        UserRoleDTO userRoleDTO= instance.useParameter(user,"参数做值");
        System.out.println(userRoleDTO);
    }

    /**多个对象*/
    @Test
    public void test2() {
        Role role = new Role(2L,"admin","超级管理员");
        user = new User(1L,"myName","myPassword","myPhone","myEmail",date,role,BigDecimal.valueOf(0.212));

        UserRoleMapper instance =  UserRoleMapper.INSTANCE;
        UserRoleDTO userRoleDTO = instance.toUserRoleDto(user,role);
        System.out.println(userRoleDTO);
    }

    /**update*/
    @Test
    public void test3() {
        Role role = new Role(2L,"admin","超级管理员");
        user = new User(1L,"myName","myPassword","myPhone","myEmail",date,role,BigDecimal.valueOf(0.212));

        UserRoleMapper instance = UserRoleMapper.INSTANCE;
        instance.update(user,new UserRoleDTO());
        System.out.println(user);
    }

    /**copy List*/
    @Test
    public void test5(){
        String dateString = "20240401";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate date = LocalDate.parse(dateString, formatter);
        System.out.println(date);
        Role role = new Role(1L, "admin", "超级管理员");
        User user = new User(1L, "myName", "myPassword", "myPhone", "myEmail",date, role,BigDecimal.valueOf(0.212));
        List<User> userList = new ArrayList<>();
        Role role1 = new Role(2L, "user", "普通用户");
        User user1 = new User(2L, "userName", "userPassword", "userPhone", "userEmail", date,role,BigDecimal.valueOf(0.212));
        userList.add(user);
        userList.add(user1);
        List<UserRoleDTO> userRoleDTOS = UserRoleMapper.INSTANCE.toUserRoleDTOList(userList);
        System.out.println(userRoleDTOS);

    }
    /** 对象中含map*/
    @Test
    public void test6(){
        Map<String,String> map = new HashMap<String,String>();
        map.put("username","xiaoming");
        map.put("phone","13412552235");
        map.put("email","xiaoming@qq.com");
        User user1 = UserRoleMapper.INSTANCE.mapToUser(map,"1");
        System.out.println(user1);
    }

    /**时间类型转换*/
    @Test
    public void test7(){
        Role role = new Role(1L, "admin", "超级管理员");
        User user = new User(1L, "myName", "myPassword", "myPhone", "myEmail",date, role,BigDecimal.valueOf(0.212));
        UserRoleDTO userRoleDTO = UserRoleMapper.INSTANCE.dtoToUserRoleDto(user,role);
        System.out.println(userRoleDTO);
    }

    @Test
    /**@AfterMapping  所有test都会调用@AfterMapping的方法，无需引用*/
    public void test8(){
        Role role = new Role(1L, "admin", "超级管理员");
        User user = new User(1L, "myName", "myPassword", "myPhone", "myEmail",date, role,BigDecimal.valueOf(0.212));
        UserRoleDTO userRoleDTO = UserRoleMapper.INSTANCE.dtoToUserRoleDto(user,role);
        System.out.println(userRoleDTO);
    }



    @Test
    public  void test9(){
        HashMap<String,BigDecimal> map = new HashMap<>();
        UpdateEstimateDTO dto= new UpdateEstimateDTO(1L,10L,8L,"备注","部门名称","1.0",BigDecimal.valueOf(0.212),BigDecimal.valueOf(0.212),BigDecimal.valueOf(0.212),map);
        EstimateMapper estimateMapper = EstimateMapper.INSTANCE;
        TimeSheetEstimate timeSheet = estimateMapper.dtoToTimeSheetEstimate(dto);
        System.out.println(timeSheet);
    }
    //嵌套对象 user-role是否可以拷贝（属性名完全一致）
    @Test
    public void test10() {
        Role role = new Role(1L, "admin", "超级管理员");
        User user = new User(1L, "myName", "myPassword", "myPhone", "myEmail",date, role,BigDecimal.valueOf(0.212));

        UserRoleMapper userRoleMapper = UserRoleMapper.INSTANCE;
        UserVo userVo = userRoleMapper.toUserVo(user);
        System.out.println(userVo);
    }

    //嵌套对象 user-role是否可以拷贝（属性名有不一致，且进行了Mapping映射）会把所有属性都getset
    //role roleDto
    @Test
    public void test11() {
        Role role = new Role(1L, "admin", "超级管理员");
        User user = new User(1L, "myName", "myPassword", "myPhone", "myEmail",date, role,BigDecimal.valueOf(0.212));

        UserRoleMapper userRoleMapper = UserRoleMapper.INSTANCE;
        UserVo1 userVo = userRoleMapper.toUserVo1(user);
        System.out.println(userVo);
    }

    //嵌套对象 user-role是否可以拷贝（属性名有不一致，不进行了Mapping映射）映射不到的置空
    @Test
    public void test12() {
        Role role = new Role(1L, "admin", "超级管理员");
        User user = new User(1L, "myName", "myPassword", "myPhone", "myEmail",date, role,BigDecimal.valueOf(0.212));

        UserRoleMapper userRoleMapper = UserRoleMapper.INSTANCE;
        UserVo1 userVo = userRoleMapper.toUserVo1UnMapping(user);
        System.out.println(userVo);
    }

    /**分别用2个mapper处理 多个对象*/
    @Test
    public void test13(){
        Role role = new Role(1L, "admin", "超级管理员");
        User user = new User(1L, "myName", "myPassword", "myPhone", "myEmail",date, role,BigDecimal.valueOf(0.212));

        UserRoleMapper userRoleMapper = UserRoleMapper.INSTANCE;
        RoleMapper roleMapper = RoleMapper.INSTANCE;

        RoleDTO roleDTO = roleMapper.toRoleDTO(role);
        UserRoleDTO userRoleDTO = userRoleMapper.toUserRoleDTO(user);
        System.out.println("userRoleDTO 中roleDTO为空"+userRoleDTO);
        userRoleDTO.setRoleDTO(roleDTO);
        System.out.println("roleDTO"+roleDTO);
        System.out.println("userRoleDTO"+userRoleDTO);
    }

    /**bigdecimal 转 string
    *如果类型不同，进行mapping映射，也可正常getset*/
    @Test
    public void test14(){
        Role role = new Role(1L, "admin", "超级管理员");
        User user = new User(1L, "myName", "myPassword", "myPhone", "myEmail",date, role,BigDecimal.valueOf(0.212));

        UserRoleMapper userRoleMapper = UserRoleMapper.INSTANCE;
        UserVo2 userVo2 = userRoleMapper.toUserVo2(user);
        System.out.println(userVo2);
    }


}
